<!--
  -    Copyright (c) 2018-2025, hycloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: hycloud
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="rowDel">
                       <template slot="menuLeft">
                            <el-button
                            icon="el-icon-circle-plus"
                            size="small"
                            v-if="bizId"
                            type="primary"
                            @click="partAssign">新增</el-button>
                        </template>
                        <template slot="duration" slot-scope="scope">
                            {{scope.row.new_duration}}
                        </template>
                        <template slot="menu" slot-scope="scope">
                            <el-button type="text"
                            icon="el-icon-edit"
                            size="small"
                            v-if="bizId && scope.row.status === '1'"
                            @click="partAssign(scope)">编辑</el-button>
                            <el-button type="text"
                            icon="el-icon-delete"
                            size="small"
                            v-if="bizId"
                            @click="rowDel(scope.row)">删除</el-button>
                            <!-- <el-button type="text"
                            size="small"
                            v-if="!bizId && scope.row.status === '1'"
                            @click="leave(scope)">出场</el-button> -->
                            <el-button type="text"
                            size="small"
                            v-if="bizId && scope.row.status === '1'"
                            @click="leave(scope)">出场</el-button>
                        </template>
            </avue-crud>
        </basic-container>
         <el-dialog
            :visible.sync="modal3"
            v-if="modal3"
            title="停车登记"
            width="1000px"
            @close="closeDialog"
            top="5vh"
            :close-on-click-modal="false"
            append-to-body>
            <div class="ck-designer">
                <el-form ref="form" :rules="rules" :model="form" label-width="100px">
                    <!-- <el-row>
                        <el-col :span="12">
                             <el-form-item label="停车场" prop="parkingId">
                                 <el-select v-model="form.parkingId"
                                            placeholder="请选择停车场"
                                            @change="changePark"
                                            clearable>
                                    <el-option v-for="item in optionsTcc"
                                            :key="item.parkingId"
                                            :label="item.name"
                                            :value="item.parkingId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                             <el-form-item label="车位" prop="spaceId">
                                 <el-select v-model="form.spaceId"
                                            placeholder="请选择车位"
                                            clearable>
                                    <el-option v-for="item in optionsCw1"
                                            :key="item.spaceId"
                                            :label="item.no"
                                            :disabled="item.status != '0'"
                                            :value="item.spaceId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row> -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="车辆类型" prop="carModel">
                                <el-select v-model="form.carModel"
                                            placeholder="请选择车辆类型"
                                            disabled
                                            clearable>
                                    <el-option v-for="item in optionsMo"
                                            :key="item.id"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                             <el-form-item label="车牌" prop="carNo">
                                 <el-input v-model="form.carNo" :minlength="6" :maxlength="12"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="品牌" prop="brand">
                                 <el-input v-model="form.brand" :maxlength="64"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                             <el-form-item label="颜色" prop="color">
                                 <el-input v-model="form.color" :maxlength="32"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="联系人" prop="contact">
                                 <el-input v-model="form.contact" :maxlength="20"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                             <el-form-item label="电话" prop="phone">
                                 <el-input v-model="form.phone" :maxlength="20"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="开始时间" prop="startTime">
                                 <el-date-picker
                                    v-model="form.startTime"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetime"
                                    placeholder="选择日期时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="车辆照片">
                                <el-upload
                                :key="keyNum"
                                    :headers="headers"
                                    :show-file-list="false"
                                    :on-success="handleAvatarSuccess"
                                    class="avatar-uploader"
                                    action="/act/file/upload"
                                    >
                                    <img
                                        v-if="form.carImage"
                                        id="avatar"
                                        :src="avatarUrl"
                                        class="avatar"
                                    />
                                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                                    </el-upload>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-form-item label="备注">
                            <el-input type="textarea" v-model="form.remark" :rows="4" :maxlength="512"></el-input>
                        </el-form-item>
                    </el-row>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="alarmHandle('form')">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
            :visible.sync="modal4"
            v-if="modal4"
            title="出场"
            width="500px"
            top="40vh"
            :close-on-click-modal="false"
            append-to-body>
            <div class="return-block">
                <el-row style="line-height: 40px">
                    <el-col :span="6">结束时间：</el-col>
                    <el-col :span="18">
                        <el-date-picker
                            v-model="params.endTime"
                            type="datetime"
                            placeholder="请选择结束时间："
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </el-col>
                </el-row>
                <el-row style="line-height: 40px">
                    <el-col :span="6">备注：</el-col>
                    <el-col :span="18">
                        <el-input type="textarea" v-model="params.remark" :rows="4" :maxlength="512"></el-input>
                    </el-col>
                </el-row>
            </div>
            <div class="btn-footer">
                <el-button type="primary" @click="leaveSubmit">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {fetchList, release, addObj, putObj, delObj} from '@/api/parking/parkingcar'
    import {fetchList as fetchList1} from '@/api/parking/parking'
    import {fetchList as fetchList2} from '@/api/parking/parkingspace'
    import {tableOption} from '@/const/crud/parking/parkingcar'
    import { remote } from "@/api/admin/dict";
    import { formatSeconds } from "@/util/date";
    import store from '@/store'
    import {mapGetters} from 'vuex'

    export default {
        props: ['bizId', 'carType'],
        name: 'parkingcar',
        data() {
            return {
                 rules: {
                    parkingId: [
                        { required: true, message: '请选择停车场' }
                    ],
                    spaceId: [
                        { required: true, message: '请选择车位' }
                    ],
                    carNo: [
                        { required: true, message: '请输入车牌', trigger: 'blur' },
                        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                    ],
                    contact: [
                        { required: true, message: '请输入联系人', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入联系电话', trigger: 'blur' }
                    ],
                    startTime: [
                        { required: true, message: '请选择开始时间' },
                    ],
                },
                params: {
                    carId: '',
                    remark: '',
                    endTime:''
                },
                avatarUrl: '',
                keyNum: 0,
                form: {
                    spaceId: ''
                },
                editFlag: false,
                optionsMo: [],
                optionsTcc: [],
                optionsCw: [],
                optionsCw1: [],
                modal3: false,
                modal4: false,
                searchForm: {},
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            headers() {
                return {
                    Authorization: "Bearer " + store.getters.access_token
                }
            },
            permissionList() {
                return {
                    addBtn: false,
                    delBtn: false,
                    editBtn: false
                };
            }
        },
        created() {
            if (this.bizId) {
                this.tableOption.menu = true
            } else {
                this.tableOption.menu = false
            }
            this.init()
        },
        methods: {
            getList(page, params) {
                this.tableLoading = true
                let att = {
                    current: page.currentPage,
                    size: page.pageSize
                }
                if (this.bizId) {
                    att.bizId = this.bizId
                }
                fetchList(Object.assign(att, params, this.searchForm )).then(response => {
                    response.data.data.records.map(item => {
                        item.new_duration = item.duration ? formatSeconds(item.duration): ''
                    })
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            init() {
                fetchList1({current: 1, size:1000, status: '1'}).then(response => {
                    this.optionsTcc = response.data.data.records
                })
                fetchList2({current: 1, size:1000}).then(response => {
                    this.optionsCw = response.data.data.records
                })
                // 车辆类型
                remote('parking_car_type').then(response => {
                    this.optionsMo = response.data.data
                })
            },
            leave(scope) {
                this.modal4 = true
                this.params.carId = scope.row.carId
            },
            alarmHandle(formName) {
                let _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.editFlag) {
                            putObj(this.form).then(data => {
                                this.$message.success('修改成功')
                                this.getList(this.page)
                                this.init()
                                this.closeDialog()
                            })
                        } else {
                            addObj(this.form).then(data => {
                                this.$message.success('登记成功')
                                this.getList(this.page)
                                this.init()
                                this.closeDialog()
                            })
                        }
                    }
                })
            },
            handleAvatarSuccess(res, file, fileList) {
                this.avatarUrl = URL.createObjectURL(file.raw)
                this.form.carImage = res.data.url
                this.keyNum += 1
            },
            close() {
                this.modal4 = false
                this.params = {
                    carId: '',
                    remark: '',
                    endTime:''
                }
            },
            leaveSubmit() {
                let _this = this
                if(!_this.params.endTime) {
                    _this.$message.error('请选择出场时间')
                    return
                }
                 this.$confirm('是否确认出场?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    release(_this.params).then(response => {
                        _this.$message.success('出场成功')
                        _this.close()
                        _this.getList(_this.page)
                    })
                })
            },
            closeDialog() {
                this.modal3 = false
                this.form = {
                    spaceId: ''
                }
            },
            changePark(value) {
                this.form.spaceId = ''
                fetchList2({current: 1, size:1000, parkingId:value}).then(response => {
                    this.optionsCw1 = response.data.data.records
                })
            },
            partAssign(scope) {
                this.editFlag = false
                this.form.bizId = this.bizId
                this.form.carModel = this.carType
                if (scope.row && scope.row.carId) {
                    this.editFlag = true
                    this.form = scope.row
                    this.avatarUrl = scope.row.carImage
                    this.optionsCw1 = this.optionsCw.filter(item => {
                        return item.parkingId === scope.row.parkingId
                    })
                }
                this.modal3 = true
            },
            rowDel: function (row, index) {
                this.$confirm('是否确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.carId)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            handleSave: function (row, done,loading) {
                addObj(row).then(data => {
                    this.$message.success('添加成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            currentChange(current){
                this.page.currentPage = current
            },
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            refreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
<style scoped  lang="scss">
/deep/.el-select, .el-input-number, .el-date-editor{
    width: 100%;
}
 /deep/.avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

/deep/.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

/deep/.avatar-uploader-icon {
    font-size: 28px !important;
    color: #8c939d !important;
    width: 178px !important;
    height: 178px !important;
    line-height: 178px !important;
    text-align: center !important;
}

/deep/.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
.btn-footer{
    text-align: center;
}
</style>